package com.ruoyi.web.controller.system;

import java.util.HashMap;
import java.util.List;

import com.ruoyi.system.domain.SelectPurchase;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Requisitions;
import com.ruoyi.system.service.IRequisitionsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 请购单Controller
 * 
 * @author ruoyi
 * @date 2021-04-30
 */
@RestController
@RequestMapping("/system/requisitions")
public class RequisitionsController extends BaseController
{
    @Autowired
    private IRequisitionsService requisitionsService;

    //查询请购单
    //@PreAuthorize("@ss.hasPermi('system:requisitions:list')")
    @GetMapping("/list")
    public TableDataInfo list(Requisitions requisitions)
    {
        if (null!=requisitions.getpState()&&!"".equals(requisitions.getpState())){

            requisitions.setState(requisitions.getpState());

        }

        startPage();
        List<Requisitions> list = requisitionsService.selectRequisitionsList(requisitions);
        return getDataTable(list);
    }

    //新增请购单
    //@PreAuthorize("@ss.hasPermi('system:requisitions:add')")
    @Log(title = "请购单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Requisitions requisitions)
    {
        int i = requisitionsService.insertRequisitions(requisitions);
        if (i==0){
            return error();
        }else {
            return success();
        }

    }


    //请购单回显
    @GetMapping("/listRequisition/{rid}")
    public AjaxResult listRequisition(@PathVariable long rid)
    {
        System.out.println(rid);
        Requisitions requisitions=requisitionsService.listRequisition(rid);
        System.out.println(requisitions);
        return AjaxResult.success(requisitions);
    }


    //删除请购单详情
    @GetMapping("/deleteRecord")
    public AjaxResult deleteRecord(long rid)
    {
        return toAjax(requisitionsService.deleteRecord(rid));
    }



    //生成采购单
    @PostMapping("/purchase")
    public AjaxResult purchase(@RequestBody Requisitions requisitions)
    {


        return toAjax(
                requisitionsService.purchase(requisitions));
    }

    //查看采购单
    @GetMapping("/selectPurchase")
    public List<SelectPurchase> selectPurchase(Long rid)
    {
        return requisitionsService.selectPurchase(rid);
    }


    /**
     * 导出请购单列表
     */

    @Log(title = "请购单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Requisitions requisitions)
    {
        if (null!=requisitions.getpState()&&!"".equals(requisitions.getpState())){

            requisitions.setState(requisitions.getpState());

        }
        List<Requisitions> list = requisitionsService.selectRequisitionsList(requisitions);
        ExcelUtil<Requisitions> util = new ExcelUtil<Requisitions>(Requisitions.class);
        return util.exportExcel(list, "请购单数据");
    }

    /**
     * 获取请购单详细信息
     */

    @GetMapping(value = "/{rid}")
    public AjaxResult getInfo(@PathVariable("rid") Long rid)
    {
        return AjaxResult.success(requisitionsService.selectRequisitionsById(rid));
    }


    /**
     * 修改请购单
     */

    @Log(title = "请购单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Requisitions requisitions)
    {
        return toAjax(requisitionsService.updateRequisitions(requisitions));
    }

    /**
     * 删除请购单
     */

    @Log(title = "请购单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{rids}")
    public AjaxResult remove(@PathVariable Long[] rids)
    {
        return toAjax(requisitionsService.deleteRequisitionsByIds(rids));
    }
}
